package com.xquant.marketdata.curvestrip.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.github.pagehelper.PageInfo;
import com.xquant.marketdata.curvestrip.mapper.DictMapper;
import com.xquant.marketdata.curvestrip.mapper.IrCurveInterpolationMapper;
import com.xquant.marketdata.curvestrip.service.IrCurveInterpolationService;
import com.xquant.marketdata.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service(value = IrCurveInterpolationService.BEAN_NAME)
public class IrCurveInterpolationServiceImpl implements IrCurveInterpolationService {

    @Autowired
    private IrCurveInterpolationMapper irCurveInterpolationMapper;

    @Autowired
    private DictMapper dictMapper;


    @Override
    public PageInfo<Map> calculateCurvesRatesByPage(Map<String, Object> param) {

        String raws = (String) param.get("raws");
        HashMap<String, Object> map = new HashMap<>();
        map.put("DICT_TYPE", "interPolationColumns");
        List<Map> dictColunms = dictMapper.queryIrDictByKey(map);//查字典中曲线

        List<String> keys = dictColunms.stream().map(element -> {
            String dictKey = (String) element.get("DICT_KEY");
            return dictKey;
        }).collect(Collectors.toList());

        JSONArray array = JSON.parseArray(raws);

        List<Map> result = new ArrayList<>();
        for (int i = 0; i < array.size(); i++) {
            JSONObject jsonObject = array.getJSONObject(i);

            Map<String, Object> params = JSON.parseObject("", new TypeReference<Map<String, Object>>() {
            });

            String ircCode = (String) params.get("IRC_CODE");
            if (StringUtils.isBlank(ircCode)) {//前端传递的曲线值为空则不对改曲线的各期限值进行计算
                break;
            }
            for (String key : keys) {
                if (params.get(key) == null) {//新增的列，计算值
                    params.put(key, Math.random());
                } else {
                    if (StringUtils.isBlank(String.valueOf(params.get(key)))) {//列的值为空
                        params.put(key, Math.random());
                    } else if (String.valueOf(params.get(key)).equals("0")) {//0重新计算
                        params.put(key, Math.random());
                    }
                }

            }

            result.add(params);
        }


        return new PageInfo<Map>(result);
    }

    @Override
    public void calculateCurvesSaveByPage(Map<String, Object> param) {
        String raws = (String) param.get("raws");

        JSONArray array = JSON.parseArray(raws);

        for (int i = 0; i < array.size(); i++) {
            JSONObject jsonObject = array.getJSONObject(i);

            Map<String, Object> params = JSON.parseObject("", new TypeReference<Map<String, Object>>() {
            });

            Set<String> keySet = params.keySet();

            HashMap<String, Object> searchParam = new HashMap<>();
            searchParam.put("ruleDate", param.get("ruleDate"));
            searchParam.put("IRC_CODE", params.get("IRC_CODE"));
            for (String key : keySet) {
                if (key.equals("IRC_CODE")) {
                    continue;
                } else {
                    searchParam.put("IRC_TERM", key);
                    List<Map> list = irCurveInterpolationMapper.queryIrCurve(searchParam);
                    if (list.size() == 0) {//没有此纪录，插入
                        HashMap<String, Object> saveParam = new HashMap<>();
                        saveParam.put("BEG_DATE", param.get("ruleDate"));
                        String tomorrow = DateUtils.addDate(String.valueOf(param.get("ruleDate")), 1);
                        saveParam.put("END_DATE", tomorrow);
                        saveParam.put("IRC_CODE", params.get("IRC_CODE"));
                        saveParam.put("IRC_TERM", key);
                        saveParam.put("IRC_RATE", params.get(key));
                        irCurveInterpolationMapper.insertIrCurve(saveParam);
                    } else {//有当条记录修改
                        Map record = list.get(0);
                        record.put("IRC_RATE", params.get(key));
                        irCurveInterpolationMapper.updateIrCurve(record);
                    }

                }
            }

        }

    }
}
